//
//  GameStartViewController.m
//  Flyer
//
//  Created by Brian Chung on 7/14/12.
//  Copyright (c) 2012 Stony Brook University Suny. All rights reserved.
//

#import "GameStartViewController.h"
#import "GameOverViewController.h"
#import "ViewController.h"
@interface GameStartViewController ()

@end

@implementation GameStartViewController
@synthesize gameOver;
@synthesize currentView;
- (void)viewDidLoad {
    [super viewDidLoad];
    int maxWidth=480;
    int maxHeight=320;
    ball = [Ball alloc];
    ball.position = CGPointMake(30.0, 160.0);
    ball.velocity = CGPointMake(00.0, 10.0);
    ball.radius = 25.0;
    ball.color = [UIColor yellowColor].CGColor;
    
    
    backgroundTop = [BackgroundObject alloc];
    //backgroundTop.radius = 30.0;
    backgroundTop.height = 50.0;
    backgroundTop.position = CGPointMake(maxWidth, 0);
    backgroundTop.velocity = CGPointMake(-5.0, 0.0);
    backgroundTop.width = 692;
    //backgroundTop.color = [UIColor greenColor].CGColor;
    
    backgroundBottom = [BackgroundObject alloc];
    backgroundBottom.radius = 30.0;
    backgroundBottom.position = CGPointMake(maxWidth-backgroundBottom.radius, maxHeight-backgroundBottom.radius);
    backgroundBottom.velocity = CGPointMake(-5.0, 0.0);
    
    
    backgroundBottom.color = [UIColor redColor].CGColor;
    
    // y-axis have 2 points. 
    for (int x=0;x<480;x++){
        for (int y=0; y<
             
             
             2; y++){
            points[x][y]=-1;
        }
    }
    points[10][1]=10;
    autoTimer=[NSTimer scheduledTimerWithTimeInterval:.05 target:self selector: @selector(onTimer) userInfo:nil repeats:YES];    
    
    
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    ball.hold=YES;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    ball.hold=NO;
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void)onTimer {
    
    CGRect rectBall = CGRectMake(ball.position.x - ball.radius, ball.position.y - ball.radius, ball.radius * 2.0, ball.radius * 2.0);
    
    CGRect rectTop = CGRectMake(backgroundTop.position.x - backgroundTop.radius, backgroundTop.position.y - backgroundTop.radius, backgroundTop.radius * 2.0, backgroundTop.radius * 2.0);
    
    CGRect rectBottom = CGRectMake(backgroundBottom.position.x - backgroundBottom.radius, backgroundBottom.position.y - backgroundBottom.radius, backgroundBottom.radius * 2.0, backgroundBottom.radius * 2.0);    
    if(ball.position.y-ball.radius>rectTop.origin.y+backgroundTop.radius*2 || ball.position.x < rectTop.origin.x- backgroundTop.radius)
    {
        
        [ball update];
        [backgroundTop update];
        [backgroundBottom update];
    }
    else{
        ball.stopFlying=YES;
        if(self.gameOver==nil){
        
        self.gameOver=[[GameOverViewController alloc] initWithNibName:@"GameOverViewController" bundle:nil];
        }
        
        //NSLog(@"check if timer stops");
        
        //stops the timer
//        [autoTimer invalidate];
//        autoTimer=nil;
        //self.view =nil;
        [gameOver setCurrentView:currentView];
        self.view=nil;
        //[self.view insertSubview:self.gameOver.view atIndex:0];
        [self.currentView.view addSubview:self.gameOver.view];
        
    }
    [(GameView *)self.view refresh:rectTop: rectBottom: rectBall: points: ball.stopFlying];     
}
@end
